package com.compass.doctor.ui.consultation.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.compass.framework.ext.toJson
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.ConsultationOrder
import com.compass.framework.model.CreateConsultation
import com.compass.framework.model.Dictionaries
import com.compass.framework.model.Doctor
import com.compass.framework.model.MeetInfo
import com.compass.framework.model.Message
import com.compass.framework.toast.TipsToast
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.flow.requestFlow
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * Description： 会诊的ViewModel
 * Date:2023/12/22 13:46
 */
class ConsultationViewModel : BaseViewModel() {
    val recommendDoctorLiveData: MutableLiveData<MutableList<Doctor>?> = MutableLiveData()
    var consultationLiveData = MutableLiveData<MutableList<ConsultationOrder>?>()
    var consultationDetailsLiveData = MutableLiveData<ConsultationOrder?>()
    var createLiveData = MutableLiveData<Boolean>()//创建成功
    var refuseOrAcceptData = MutableLiveData<Boolean>()//拒绝/接受/取消
    var passwordLiveData = MutableLiveData<MeetInfo?>()//会议密码


    //医生接受或拒绝会诊
    //status 0是拒绝 1是接受
    fun refuseOrAccept(orderId :String , status: Int ) {
        val params = JsonObject().apply {
            addProperty("id",orderId)
            addProperty("status",status)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.refuseOrAccept(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                refuseOrAcceptData.value = false
            }
        }, successBlock = {
            it?.let {
                refuseOrAcceptData.value = true
            }
        })
    }


    //结束问诊
    fun finishConsultation(id :String ,report:String) {
        val params = JsonObject().apply {
            addProperty("id",id)
            addProperty("report",report)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.finishConsultation(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                refuseOrAcceptData.value = false
            }
        }, successBlock = {
            it?.let {
                refuseOrAcceptData.value = true
            }
        })
    }


    fun getConsultationList(mPage: Int, pageSize: Int = 10, type: Int) {

        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationList(mPage, pageSize, type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                consultationLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                consultationLiveData.value = null
            }else{
                consultationLiveData.value=it.list
            }
        })
    }

    fun getConsultationDetails(id:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationDetails(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                consultationDetailsLiveData.value = null
            }
        }, successBlock = {
            it?.let {
                consultationDetailsLiveData.value=it
            }
        })
    }

    fun cancelConsultation(id:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.cancelConsultation(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                refuseOrAcceptData.value = false
            }
        }, successBlock = {
            it?.let {
                refuseOrAcceptData.value = true
            }
        })
    }

    /**
     * 获取入会信息
     */
    fun getConsultationPassword(id:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationPassword(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                passwordLiveData.value = null
            }
        }, successBlock = {
            passwordLiveData.value=it
        })
    }


    fun createConsultation(createConsultation: CreateConsultation?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.createConsultation(createConsultation)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                createLiveData.value = false
            }
        }, successBlock = {
            it?.let {
                createLiveData.value = true
            }

        })

    }


    fun recommendDoctor() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.recommendDoctor()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                recommendDoctorLiveData.value=null
            }
        }, successBlock = {
            recommendDoctorLiveData.value=it

        })

    }




}